package com.phpxiu.app.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;

import com.phpxiu.app.kkylive.R;

import java.io.File;


/**
 * @author 吴金洲
 * @file
 * @brief 编辑图片的菜单
 * @note 底部弹出菜单选项：相册和拍照
 */
public class PhotoOptionMenus extends Dialog implements View.OnClickListener {
    public final static String TAG = "PhotoOptionMenus";
    public final static String CACHE= Environment.getExternalStoragePublicDirectory("/").getPath()+"/com.phpxiu.app.kky/photo/";
    public static final String TEMP_PHOTO_NAME = "temp_capture_photo.png";//拍照图片名
    public final static int REQUEST_PHOTO_CODE=0x5227;//获取一张图片
    public final static int REQUEST_CAPTURE_CODE=0x5228;//拍取一张图片
    public final static int REQUEST_PHOTO_CROP_CODE=0x5229;//获取一张图片用于剪切
    public final static int REQUEST_CAPTURE_CROP_CODE=0x5230;//拍取一张图片用于剪切
    public final static int REQUEST_CROP_CODE=0x5231;//请求剪切一张图片
    public final static int REQUEST_PREVIEW_CODE=0x5232;//请求预览图片

    private Fragment fragment=null;
    private String title;//图片系统选择方式标题
    private boolean isCrop=false;//默认不剪切图片
    private Uri tempUri=null;
    public PhotoOptionMenus(@NonNull Fragment f) {
        super(f.getActivity(), R.style.menusDialog);
        fragment=f;
        title=f.getString(R.string.system_photo_title);
        init();
    }

    public PhotoOptionMenus(Activity activity) {
        super(activity, R.style.menusDialog);
        setOwnerActivity(activity);
        fragment=null;
        title=activity.getString(R.string.system_photo_title);
        init();
    }

    /**
     * 初始化视图
     */
    private void init() {
        setContentView(R.layout.photo_option_menus);
        findViewById(R.id.root_view).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
        findViewById(R.id.open_album).setOnClickListener(this);
        findViewById(R.id.open_camera).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open_album:
                onAlbumAction();
                break;
            case R.id.open_camera:
                onCameraAction();
                break;
            default:
                break;
        }
        dismiss();
    }

    public void show(boolean crop) {
        isCrop=crop;
        super.show();
    }

    /**
     * 从相册选图
     */
    final private void onAlbumAction(){
        Intent album = new Intent();
        album.setType("image/*");
        album.setAction(Intent.ACTION_GET_CONTENT);
        album.addCategory(Intent.CATEGORY_OPENABLE);
        int requestCode=REQUEST_PHOTO_CODE;
        if(isCrop){
            requestCode=REQUEST_PHOTO_CROP_CODE;
        }
        if(fragment!=null){
            fragment.startActivityForResult(Intent.createChooser(album,title), requestCode);
        }else{
            getOwnerActivity().startActivityForResult(Intent.createChooser(album,title), requestCode);
        }
    }

    /**
     * 从相机选图
     */
    final private void onCameraAction(){
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(tempUri==null){
            File dir=new File(CACHE);
            if(!dir.exists()){
                dir.mkdirs();
            }
            File file=new File(dir,TEMP_PHOTO_NAME);
            tempUri=Uri.fromFile(file);
        }
        if(tempUri==null){
            return;
        }
        int requestCode=REQUEST_CAPTURE_CODE;
        if(isCrop){
            requestCode=REQUEST_CAPTURE_CROP_CODE;
        }
        camera.putExtra(MediaStore.EXTRA_OUTPUT,tempUri);
        if(fragment!=null){
            fragment.startActivityForResult(Intent.createChooser(camera,title), requestCode);
        }else{
            getOwnerActivity().startActivityForResult(Intent.createChooser(camera,title), requestCode);
        }
    }

    public Uri getTempUri() {
        return tempUri;
    }
}
